/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArbitratorNagotiationPanel.java
 *
 * Created on 06/08/2011, 16:28:02
 */
package GUI;

import common.SLA;
import common.ServerResponse;
import common.Settings;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import service.arbitrator.ArbitratorDetails;

/**
 *
 * @author Danny
 */
public class ArbitratorNagotiationPanel extends javax.swing.JPanel {

    Controller controller;

    /** Creates new form ArbitratorNagotiationPanel */
    public ArbitratorNagotiationPanel() {
        initComponents();
    }

    ArbitratorNagotiationPanel(Controller controller) {
        this.controller = controller;
        initComponents();
        jLabel3.setText("Choose at least " + Settings.sizeOfAgreementSet + " arbitrators");
        this.jButton1.setEnabled(false);
        jLabel1.setVisible(false);//         setText("NO Agreement");
        jButton2.setEnabled(false);
        jButton2.setToolTipText("Connect to the forum");

        //ArbitratorDetails[] ad = controller.abs.getArbitrators();
        ArbitratorDetails[] ad = controller.user.getAllAvailableArbitrators();

        ArbitratorsTableModel atm = new ArbitratorsTableModel(ad, jButton1);
        jTable1.setModel(atm);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane4 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jTable1.setModel(new ArbitratorsTableModel());
        jScrollPane4.setViewportView(jTable1);

        jButton1.setText("Send request");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jButton2.setText("Finish");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        jLabel2.setText("Choose arbitrators");

        jLabel3.setText("jLabel3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap(271, Short.MAX_VALUE))
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(399, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Set userAcceptableArbitratorsSet = ((ArbitratorsTableModel) jTable1.getModel()).userAcceptableArbitrators;
        controller.user.userAcceptableArbitrators = (Integer[]) userAcceptableArbitratorsSet.toArray(new Integer[0]);
        ServerResponse sr = controller.user.startSessionWithServer(controller.user.userAcceptableArbitrators);

        List<Integer> list = Arrays.asList((Integer[]) sr.getIntData());
        Set<Integer> set = new HashSet<Integer>(list);
        ((ArbitratorsTableModel) jTable1.getModel()).serverAcceptableArbitrators = new HashSet<Integer>(set);
        jTable1.repaint();
        String chooseAgainString = "Should agree on at least " + Settings.sizeOfAgreementSet + " arbitrators.\n Please choose again.";
        Object acceptSLA = "Server's SLA\n"+SLA.getAgrrement()+ "Do you accept SLA?";
        if (!sr.getActionSucceeded()) {
            jLabel1.setText("NO Agreement, choose again");
            jLabel1.setVisible(true);
            controller.user.serverAcceptableArbitrator = (Integer[]) sr.getIntData();
            jButton2.setEnabled(false);
            JOptionPane.showMessageDialog(this, chooseAgainString,
                    "NO Agreement",
                    JOptionPane.ERROR_MESSAGE);

        } else {
            jLabel1.setText("Agreement achieved");
            jLabel1.setVisible(true);
            int n = JOptionPane.showConfirmDialog(
                    this,
                    acceptSLA,
                    "Agreement achieved",
                    JOptionPane.YES_NO_OPTION);
            if (n==0){
                jButton2.setEnabled(true);
            }
            if (n==1){
                jLabel1.setText("Agreement achieved. Accept SLA in order to receive service");
                jButton2.setEnabled(false);
            }

           
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        controller.user.getPrivateKeysFromArbitrators();        
        controller.showForum();
        this.jTable1.setEnabled(false);
        this.jTable1.clearSelection();                
        this.jButton1.setEnabled(false);
        this.jButton2.setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
